/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.hibernate.dao;

import com.cashmate.hibernate.pojo.*;
import java.util.HashMap;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;

/**
 *
 * @author Dine
 */
public class BorrowerDAO extends DAO {

    public BorrowerDAO() {
        super();
    }

    //BASIC DAO METHODS
    @SuppressWarnings("unchecked")
    public List<Borrowers> getAll() throws Exception {
        List<Borrowers> a = (List<Borrowers>) getSession().createQuery("from Borrowers a").setCacheable(true).list();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public Borrowers get(int id) throws Exception {
        Borrowers a = (Borrowers) getSession().createQuery("from Borrowers a where a.borrowerid=:id").setParameter("id", id).setCacheable(true).uniqueResult();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public void update(Borrowers a) throws Exception {
        try {
            begin();
            getSession().update(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void insert(Borrowers a) throws Exception {
        try {
            begin();
            getSession().save(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void delete(int id) throws Exception {
        try {
            begin();
            getSession().delete(get(id));
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Borrowers> findByCriteria(DetachedCriteria dc, int from, int size) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setFirstResult(from);
        criteria.setMaxResults(size);
        criteria.setCacheable(true);
        List<Borrowers> list = criteria.list();
        return list;
    }

    @SuppressWarnings("unchecked")
    public int countByCriteria(DetachedCriteria dc) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        criteria.setProjection(Projections.rowCount());
        int count = ((Integer) criteria.list().get(0)).intValue();
        flush();
        return count;
    }

    //OTHER METHODS
    @SuppressWarnings("unchecked")
    public boolean hasDuplicate(int borrowerid, int branchid) throws Exception {
        Borrowers b = (Borrowers) getSession().createQuery("from Borrowers w inner join w.branches b where w.borrowerid=:id and b.branchid=:branchid").setParameter("id", borrowerid).setParameter("branchid", branchid).uniqueResult();
        if (b == null) {
            return false;
        }
        return true;
    }

// use update(Borrower a) above....
//    @SuppressWarnings("unchecked")
//    public boolean updateBorrower(Borrowers b, int borid) throws Exception{
//        try {
//            getSession().createQuery("update Borrowers b set b.firstname=:fn, b.middlename=:mn, b.lastname=:ln, "
//                    + "b.religion=:rel, b.civilstatus=:cstat, b.dateofbirth=:dob, b.age=:a, b.placeofbirth=:pob,"
//                    + "b.citizenship=:cit, b.streetaddress=:sa, b.barangay=:b, b.cityormunicipality=:com,"
//                    + "b.province=:p, b.postalcode=:pc where b.borrowerid=:bid").setParameter("fn", b.getFirstname()).setParameter("mn", b.getMiddlename()).setParameter("ln", b.getLastname()).setParameter("dob", b.getDateofbirth()).setParameter("rel", b.getReligion()).setParameter("cstat", b.getCivilstatus()).setParameter("a", b.getAge()).setParameter("pob", b.getPlaceofbirth()).setParameter("cit", b.getCitizenship()).setParameter("sa", b.getStreetaddress()).setParameter("b", b.getBarangay()).setParameter("com", b.getCityormunicipality()).setParameter("p", b.getProvince()).setParameter("pc", b.getPostalcode()).setParameter("bid", b.getBranches().getBranchid()).setParameter("bid", borid).executeUpdate();
//            return true;
//        } catch (Exception ex) {
//            roll
//            return false;
//        }
//
//    }
    @SuppressWarnings("unchecked")
    public boolean addForOldBorrower(Documents d, Loanapplications la, Monthlyincome mi, List<Businesses> lb,
            List<Characterreferences> lc, List<Dependents> ld, List<Loanswithotherbanks> ll, List<LoanappRequirement> lr) throws Exception {
        try {
            begin();
            getSession().save(d);
            getSession().save(la);
            getSession().save(mi);
            for (int i = 0; i < lb.size(); i++) {
                getSession().save(lb.get(i));
            }
            for (int i = 0; i < lc.size(); i++) {
                getSession().save(lc.get(i));
            }
            for (int i = 0; i < ld.size(); i++) {
                getSession().save(ld.get(i));
            }
            for (int i = 0; i < ll.size(); i++) {
                getSession().save(ll.get(i));
            }
            for (int i = 0; i < lr.size(); i++) {
                getSession().save(lr.get(i));
            }
            commit();
            flush();
            return true;
        } catch (Exception ex) {
            rollback();
            throw ex;
        }
    }

    @SuppressWarnings("unchecked")
    public boolean addForNewBorrower(Borrowers b, Documents d, Loanapplications la, Monthlyincome mi, List<Businesses> lb,
            List<Characterreferences> lc, List<Dependents> ld, List<Loanswithotherbanks> ll, List<LoanappRequirement> lr) throws Exception {
        try {
            Session ses = getSession();
            begin();
            ses.save(b);
            ses.save(d);
            ses.save(la);
            ses.save(mi);
            for (int i = 0; i < lb.size(); i++) {
                ses.save(lb.get(i));
            }
            for (int i = 0; i < lc.size(); i++) {
                ses.save(lc.get(i));
            }
            for (int i = 0; i < ld.size(); i++) {
                ses.save(ld.get(i));
            }
            for (int i = 0; i < ll.size(); i++) {
                ses.save(ll.get(i));
            }
            for (int i = 0; i < lr.size(); i++) {
                ses.save(lr.get(i));
            }
            commit();
            flush();
            return true;
        } catch (Exception ex) {
            rollback();
            throw ex;
        }
    }

    @SuppressWarnings("unchecked")
    public HashMap<Integer, Borrowers> getBorrowersPerArea(int aid) throws Exception {
        HashMap<Integer, Borrowers> map = new HashMap<Integer, Borrowers>();
        List<Borrowers> list = (List<Borrowers>) getSession().createQuery("from Borrowers b where b.areas.areaid=:aid").
                setParameter("aid", aid).setCacheable(true).list();
        flush();
        for (Borrowers u : list) {
            map.put(u.getBorrowerid(), u);
        }
        return map;
    }

    @SuppressWarnings("unchecked")
    public List<Borrowers> getBorrowerPerAreaList(int aid) throws Exception {
        List<Borrowers> bor = (List<Borrowers>) getSession().createQuery("from Borrowers b where b.areas.areaid=:aid").setParameter("aid", aid).setCacheable(true).list();
        flush();
        return bor;
    }

    @SuppressWarnings("unchecked")
    public List<Borrowers> getActiveBorrowers() throws Exception {
        List<Borrowers> bor = (List<Borrowers>) getSession().createQuery("from Borrowers b where b.isactive=true").setCacheable(true).list();
        flush();
        return bor;
    }

    @SuppressWarnings("unchecked")
    public Borrowers getBorrowerByName(String lname, String fname) throws Exception {
        Borrowers bor = (Borrowers) getSession().createQuery("from Borrowers b where b.lastname=:lname and b.firstname=:fname").setParameter("lname", lname).setParameter("fname", fname).setCacheable(true).uniqueResult();
        flush();
        return bor;
    }
}
